package net.maku.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.framework.security.user.SecurityUser;
import net.maku.project.convert.ProjectReportConvert;
import net.maku.project.dao.ProjectReportDao;
import net.maku.project.entity.InfoEntity;
import net.maku.project.entity.ProjectReportEntity;
import net.maku.project.query.ProjectReportQuery;
import net.maku.project.service.InfoService;
import net.maku.project.service.ProjectReportService;
import net.maku.project.vo.ProjectReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @FileName: ProjectReportServiceImpl
 * @Project: maku-boot-zzhl
 * @Package: net.maku.project.service
 * @Author: Aili
 * @Date: 2024/7/22 20:51
 * @Version: 1.0.0
 * @Description: ProjectReportServiceImpl Description
 */
@Service
@AllArgsConstructor
public class ProjectReportServiceImpl extends BaseServiceImpl<ProjectReportDao, ProjectReportEntity> implements ProjectReportService {

    @Autowired
    private ProjectReportDao projectReportDao;

    private InfoService infoService;

    @Override
    public PageResult<ProjectReportVO> page(ProjectReportQuery query) {
        IPage<ProjectReportEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        return new PageResult<>(ProjectReportConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<ProjectReportEntity> getWrapper(ProjectReportQuery query) {
        LambdaQueryWrapper<ProjectReportEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(query.getProjectId()), ProjectReportEntity::getProjectId, query.getProjectId())
                .orderByDesc(ProjectReportEntity::getCreateTime);
        return wrapper;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return projectReportDao.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ProjectReportEntity record) {
       InfoEntity info = infoService.getById(record.getProjectId());
        if (Objects.isNull(info)) {
            return 0;
        }
        record.setCreator(Objects.requireNonNull(SecurityUser.getUser()).getRealName());
        record.setCreateTime(LocalDateTime.now());
        projectReportDao.insert(record);
        info.setWeeklyProgress(record.getProgressReport());
        infoService.updateById(info);
        return 1;
    }

    @Override
    public int insertSelective(ProjectReportEntity record) {
        return projectReportDao.insertSelective(record);
    }

    @Override
    public ProjectReportEntity selectByPrimaryKey(Long id) {
        return projectReportDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ProjectReportEntity record) {
        return projectReportDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ProjectReportEntity record) {
        return projectReportDao.updateByPrimaryKey(record);
    }

    @Override
    public List<ProjectReportVO> findAllByProjectId(Long projectId) {
        List<ProjectReportEntity> entityList = projectReportDao.findAllByProjectId(projectId);
        return ProjectReportConvert.INSTANCE.convertList(entityList);
    }


    @Override
    public ProjectReportEntity getById(Serializable id) {
        return super.getById(id);
    }
}
